package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.PersonalPublishBean;
import com.beiing.tianshuai.tianshuai.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.beiing.tianshuai.tianshuai.app.HttpRequestConstant.ACTIVITY_PIC_HEAD;
import static com.beiing.tianshuai.tianshuai.app.HttpRequestConstant.FRESH_COVER_URL;
import static com.beiing.tianshuai.tianshuai.app.HttpRequestConstant.VIDEO_PIC_HEAD;

/**
 * Created by Administrator on 2017/12/25 0025.
 */

public class PersonalPublishInterestAdapter extends BaseRecyclerViewAdapter<PersonalPublishBean.DataBeanX.DataBean> {

    private Context mContext;
    private int mType;
    private final RequestOptions mRequestOptions;
    private OnMoreClickListener mMoreClickListener;
    private OnItemClickListener mItemClickListener;

    public void setMoreClickListener(OnMoreClickListener moreClickListener) {
        mMoreClickListener = moreClickListener;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public PersonalPublishInterestAdapter(Context context, int type) {
        mContext = context;
        mType = type;
        mRequestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(420, 240);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = getConvertView(mContext, R.layout.item_personal_publish_interest, null);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            PersonalPublishBean.DataBeanX.DataBean bean = mList.get(position);
            ((ViewHolder) holder).setData(bean, mType);
            ((ViewHolder) holder).setListener(bean, mType);
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_cover)
        QMUIRadiusImageView mIvCover;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_publish_time)
        TextView mTvPublishTime;
        @BindView(R.id.tv_view_count)
        TextView mTvViewCount;
        @BindView(R.id.tv_reply_count)
        TextView mTvReplyCount;
        @BindView(R.id.btn_more)
        ImageButton mBtnMore;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(PersonalPublishBean.DataBeanX.DataBean bean, int type) {
            switch (type) {
                case 0:
                    mTvTitle.setText(bean.getTitle());
                    if (!TextUtils.isEmpty(bean.getCreateTime())) {
                        mTvPublishTime.setText(TimeUtils.getDateToString(Long.parseLong(bean.getCreateTime()) * 1000));
                    }
                    mTvViewCount.setText(bean.getView());
                    mTvReplyCount.setText(bean.getComment());
                    Glide.with(mContext.getApplicationContext())
                            .load(VIDEO_PIC_HEAD + bean.getCover())
                            .apply(mRequestOptions)
                            .into(mIvCover);
                    break;
                case 1:
                    mTvTitle.setText(bean.getStrech());
                    if (!TextUtils.isEmpty(bean.getCreateTime())) {
                        mTvPublishTime.setText(TimeUtils.getDateToString(Long.parseLong(bean.getCreateTime()) * 1000));
                    }
                    mTvViewCount.setText(bean.getView());
                    mTvReplyCount.setText(bean.getComment());
                    Glide.with(mContext.getApplicationContext())
                            .load(ACTIVITY_PIC_HEAD + bean.getCover())
                            .apply(mRequestOptions)
                            .into(mIvCover);
                    break;
                case 2:
                    mTvTitle.setText(bean.getTitle());
                    if (!TextUtils.isEmpty(bean.getCreateTime())) {
                        mTvPublishTime.setText(TimeUtils.getDateToString(Long.parseLong(bean.getCreateTime()) * 1000));
                    }
                    mTvViewCount.setText(bean.getView());
                    mTvReplyCount.setText(bean.getReview());
                    String cover = bean.getCover();
                    if (!TextUtils.isEmpty(cover)) {
                        mIvCover.setVisibility(View.VISIBLE);
                        String[] split = cover.split(",");
                        Glide.with(mContext.getApplicationContext())
                                .load(FRESH_COVER_URL + split[0])
                                .apply(mRequestOptions)
                                .into(mIvCover);
                    } else {
                        mIvCover.setVisibility(View.GONE);
                    }
                    break;
            }
        }

        public void setListener(final PersonalPublishBean.DataBeanX.DataBean bean, final int type) {
            mBtnMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mMoreClickListener != null) {
                        mMoreClickListener.onMoreClick(view, bean, getAdapterPosition(), type);
                    }
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(bean, getAdapterPosition(), type);
                    }
                }
            });
        }
    }

    public interface OnMoreClickListener {
        /**
         * 更多菜单点击事件
         *
         * @param bean     数据实体
         * @param position 当前列表中的位置
         * @param type     数据分类 0-兴趣 1-活动 2-发现
         */
        void onMoreClick(View view, PersonalPublishBean.DataBeanX.DataBean bean, int position, int type);
    }

    public interface OnItemClickListener {
        /**
         * 条目点击事件
         *
         * @param bean     数据实体
         * @param position 当前列表中的位置
         * @param type     数据分类 0-兴趣 1-活动 2-发现
         */
        void onItemClick(PersonalPublishBean.DataBeanX.DataBean bean, int position, int type);
    }
}
